package com.hariro.adapter;

import java.util.ArrayList;
import java.util.List;

import com.hariro.bean.SortTab;
import com.qianfeng.project.R;

import android.content.Context;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.GridView;
import android.widget.TextView;

public abstract class SortTabAdapter extends BaseAdapter {

	/**
	 * list<Object> 0--String cateName 1--String cateId 2--SortTab sortTab
	 * 3--SortTab sortTab 4--SortTab sortTab 5--SortTab sortTab 6--SortTab
	 * sortTab ......
	 */

	private List<List<Object>> list;
	private Context context;
	protected int[] imageIds;

	public SortTabAdapter(Context context, List<List<Object>> list) {
		this.list = list;
		this.context = context;
		imageIds = new int[] { R.drawable.adapter_home_menu_right_item_up,
				R.drawable.adapter_home_menu_right_item_down };
	}

	@Override
	public int getCount() {
		return list.size();
	}

	@Override
	public Object getItem(int position) {
		return list.get(position);
	}

	@Override
	public long getItemId(int position) {
		return position;
	}

	@Override
	public View getView(int position, View convertView, ViewGroup parent) {
		View view = View.inflate(context, R.layout.listview_item_sorttab, null);
		String sortText = list.get(position).get(0).toString();
		if ("".equals(sortText)) {
			view.findViewById(R.id.linearLayout_sortText).setVisibility(View.GONE);
			;
		} else {
			TextView textView = (TextView) view.findViewById(R.id.tv_sortText);
			textView.setText(list.get(position).get(0).toString());
		}
		GridView gridView = (GridView) view.findViewById(R.id.gridView_sortTab);
		gridView.setAdapter(new SortTabGridViewAdapter(context, getList(list.get(position))) {

			@Override
			public View initView(int position, View convertView, ViewGroup parent, List<SortTab> list) {
				return initView1(position, convertView, parent, list);
			}

		});

		/*
		 * 利用自定义的GridView解决了高度问题，设置的是gridView的高度不滑动，全部展示
		 * AbsListView.LayoutParams lp = new AbsListView.LayoutParams(
		 * LinearLayout.LayoutParams.MATCH_PARENT,LinearLayout.LayoutParams.
		 * MATCH_PARENT); lp.height =700; view.setLayoutParams(lp);
		 */
		return view;
	}

	private List<SortTab> getList(List<Object> list) {
		List<SortTab> list2 = new ArrayList<SortTab>();
		for (int i = 2; i < list.size(); i++) {
			list2.add((SortTab) list.get(i));
		}
		return list2;
	}

	public abstract View initView1(int position, View convertView, ViewGroup parent, List<SortTab> list);
}
